package edu.sysu.cshare.control;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;

import edu.sysu.cshare.model.Clazz;
import edu.sysu.cshare.model.Course;
import edu.sysu.cshare.model.Group;
import edu.sysu.cshare.model.User;
import edu.sysu.cshare.util.HibernateSessionFactory;

public class ClassManager {

	// TODO 权限问题
	public List<Course> getCourses() {
		return actor.getClazz().getCourses();
	}

	public static Clazz getClazzById(long id) {
		return (Clazz) HibernateSessionFactory.getSession().load(Clazz.class,
				id);
	}

	public static Course getCourseById(long id) {
		return (Course) HibernateSessionFactory.getSession().load(Course.class,
				id);
	}

	public static Group getGroupById(long id) {
		return (Group) HibernateSessionFactory.getSession().load(Group.class,
				id);
	}

	public static User getUserById(String id) {
		return (User) HibernateSessionFactory.getSession().load(User.class, id);
	}

	/**	
	 * 返回一个课程未分组的学生列表
	 * 
	 * @param course
	 *            指定的课程
	 * @return
	 */
	public static List<User> getUnGrouppedStudents(Course course) {
		List<User> result = new ArrayList<User>(course.getStudents());

		ArrayList<User> groupped = new ArrayList<User>();
		for (Group g : course.getGroups()) {
			groupped.addAll(g.getMembers());
		}
		result.removeAll(groupped);
		return result;

	}

	User actor;

	public User getActor() {
		return actor;
	}

	public ClassManager(User actor) {
		super();
		this.actor = actor;
	}

	public void addCourse(Clazz clazz, Course course) {
		Session ss = HibernateSessionFactory.getSession();
		ss.beginTransaction();
		clazz.getCourses().add(course);
		ss.save(clazz);
		ss.getTransaction().commit();
	}

	public void addUser(Clazz clazz, User user) {
		Session ss = HibernateSessionFactory.getSession();
		ss.beginTransaction();
		clazz.getStudents().add(user);
		ss.save(clazz);
		ss.getTransaction().commit();
	}

	/**
	 * 从班级中移除一个User. 删除用户的操作有点无理
	 * 
	 * @param clazz
	 * @param user
	 */
	public void deleteUser(Clazz clazz, User user) {
		Session ss = HibernateSessionFactory.getSession();
		ss.beginTransaction();
		clazz.getStudents().remove(user);
		ss.save(clazz);
		ss.getTransaction().commit();
	}

	/**
	 * 获得班级内所有的课程
	 * 
	 * @return 班级内所有的课程
	 */
	/*
	public List<Course> getAllCourse(Clazz clazz) {
		Session ss = HibernateSessionFactory.getSession();
		ss.beginTransaction();
		return ss.createQuery("from Course c where c.clazz=:clazz")
				.setEntity("clazz", clazz).list();
		return clazz.getCourses();
	}
	*/

	public List<Group> getAllGroups() {
		return null;

	}

	/**
	 * 返回用户能加入的组
	 * 
	 * @return
	 */
	public List<Group> getAvailableGroups() {
		ArrayList<Group> result = new ArrayList<Group>();
		for (Course course : actor.getCourses()) {
			if (!actor.isGroupped(course)) {
				result.addAll(course.getGroups());
			}
		}
		return result;
	}

}
